<template>
    <main class="main-page login-page">
        <div class="login-background">
            <div id="particles-js"></div>
            <div class="animated-shapes">
                <div class="shape shape1"></div>
                <div class="shape shape2"></div>
                <div class="shape shape3"></div>
            </div>
        </div>
        <section class="page-section">
            <div class="container-fluid">
                <div class="grid justify-content-center">
                    <div class="col-12 sm:col-6 md:col-4 comp-grid">
                        <div class="login-card">
                            <div class="login-header">
                                <div class="logo-icon">生</div>
                                <h1 class="text-2xl font-bold text-primary mt-3">学生注册</h1>
                                <p class="text-500 mt-2">区块链教育认证平台</p>
                            </div>
                            <div class="login-form">
                                <form ref="observer" tag="form" @submit.prevent="submitForm()">
                                    <div class="mb-2 p-input-icon-right w-full">
                                        <i class="pi pi-user"></i>
                                        <InputText :label="$t('name')" :placeholder="$t('name')" class="w-full" v-model.trim="formData.name" required="required" type="text" />
                                    </div>
                                    <div class="mb-2 p-input-icon-right w-full">
                                        <i class="pi pi-phone"></i>
                                        <InputText :label="$t('phoneNumber')" :placeholder="$t('phoneNumber')" class="w-full" v-model.trim="formData.phone_number" required="required" type="text" />
                                    </div>
                                    <div class="mb-2 p-input-icon-right w-full">
                                        <i class="pi pi-envelope"></i>
                                        <InputText :label="$t('email')" :placeholder="$t('email')" class="w-full" v-model.trim="formData.email" required="required" type="email" />
                                    </div>
                                    <div class="mb-2 p-input-icon-left w-full">
                                        <i class="pi pi-lock"></i>
                                        <Password :label="$t('password')" inputClass="w-full" :feedback="false" toggleMask class="w-full" :placeholder="$t('password')" required="required" v-model="formData.password" />
                                    </div>
                                    <div class="mb-2 p-input-icon-left w-full">
                                        <i class="pi pi-lock"></i>
                                        <Password :label="$t('confirmPassword')" inputClass="w-full" :feedback="false" toggleMask class="w-full" :placeholder="$t('confirmPassword')" required="required" v-model="formData.confirm_password" />
                                    </div>
                                    <Message v-if="errorMsg" severity="error" :closable="true" @close="errorMsg=null">
                                        {{ errorMsg.toString() }}
                                    </Message>
                                    <div class="text-center">
                                        <Button :label="$t('register')" :loading="loading" icon="pi pi-user-plus" class="p-button-lg p-button-raised w-full" type="submit">
                                        </Button>
                                    </div>
                                </form>
                            </div>
                            <div class="login-footer">
                                <div class="text-center">
                                    <router-link to="/">
                                        <Button icon="pi pi-arrow-left" :label="$t('backToLogin')" class="p-button-text" />
                                    </router-link>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </main>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue';
import { useApp } from 'src/composables/app';
import { useRouter } from 'vue-router';
import axios from 'axios';
import 'particles.js';

const router = useRouter();
const app = useApp();
const formData = reactive({
    name: '',
    phone_number: '',
    email: '',
    password: '',
    confirm_password: ''
});

const loading = ref(false);
const errorMsg = ref('');

async function submitForm() {
    try {
        if (formData.password !== formData.confirm_password) {
            errorMsg.value = '两次输入的密码不一致';
            return;
        }

        loading.value = true;
        errorMsg.value = '';

        const requestData = {
            name: formData.name,
            phone_number: formData.phone_number,
            email: formData.email,
            password: formData.password,
            confirm_password: formData.confirm_password
        };

        console.log('Sending registration request:', requestData);


        const response = await axios.post('/auth/student-register', requestData);
        
        if (response.data.success) {
            app.flashMsg('注册成功', response.data.message || '注册成功', 'success');
            router.push('/');
        } else {
            errorMsg.value = response.data.message || '注册失败，请稍后重试';
        }
    } catch (error) {
        console.error('Registration error:', error);
        errorMsg.value = error.response?.data?.message || '注册失败，请稍后重试';
    } finally {
        loading.value = false;
    }
}

onMounted(() => {
    if (typeof window.particlesJS !== 'undefined') {
        window.particlesJS('particles-js', {
            particles: {
                number: {
                    value: 100,
                    density: {
                        enable: true,
                        value_area: 800
                    }
                },
                color: {
                    value: ['#4d7cfe', '#6c63ff', '#3ae374']
                },
                shape: {
                    type: 'circle',
                    stroke: {
                        width: 0,
                        color: '#000000'
                    }
                },
                opacity: {
                    value: 0.6,
                    random: true,
                    anim: {
                        enable: true,
                        speed: 1,
                        opacity_min: 0.1,
                        sync: false
                    }
                },
                size: {
                    value: 4,
                    random: true,
                    anim: {
                        enable: true,
                        speed: 2,
                        size_min: 0.1,
                        sync: false
                    }
                },
                line_linked: {
                    enable: true,
                    distance: 150,
                    color: '#4d7cfe',
                    opacity: 0.4,
                    width: 1
                },
                move: {
                    enable: true,
                    speed: 3,
                    direction: 'none',
                    random: true,
                    straight: false,
                    out_mode: 'out',
                    bounce: false,
                    attract: {
                        enable: true,
                        rotateX: 600,
                        rotateY: 1200
                    }
                }
            },
            interactivity: {
                detect_on: 'canvas',
                events: {
                    onhover: {
                        enable: true,
                        mode: 'grab'
                    },
                    onclick: {
                        enable: true,
                        mode: 'push'
                    },
                    resize: true
                },
                modes: {
                    grab: {
                        distance: 180,
                        line_linked: {
                            opacity: 0.8
                        }
                    },
                    push: {
                        particles_nb: 6
                    }
                }
            },
            retina_detect: true
        });
    } else {
        console.warn('particles.js is not loaded');
    }
});
</script>

<style lang="scss">
:root {
    --primary: #4d7cfe;
    --secondary: #6c63ff;
    --accent: #3ae374;
    --light: #f9fafe;
    --dark: #2c3e50;
    --gradient: linear-gradient(135deg, #6c63ff, #4d7cfe);
}

.login-page {
    min-height: 100vh;
    position: relative;
    overflow: hidden;
}

.login-background {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 0;
}

#particles-js {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    z-index: 1;
    pointer-events: none;
}

.animated-shapes {
    position: absolute;
    width: 100%;
    height: 100%;
    overflow: hidden;
}

.shape {
    position: absolute;
    background: linear-gradient(45deg, var(--primary), var(--secondary));
    border-radius: 50%;
    opacity: 0.1;
    animation: float 15s infinite ease-in-out;

    &.shape1 {
        width: 300px;
        height: 300px;
        top: -150px;
        left: -150px;
        animation-delay: 0s;
    }

    &.shape2 {
        width: 200px;
        height: 200px;
        top: 50%;
        right: -100px;
        animation-delay: -5s;
    }

    &.shape3 {
        width: 150px;
        height: 150px;
        bottom: -75px;
        left: 50%;
        animation-delay: -10s;
    }
}

.login-card {
    background: rgba(255, 255, 255, 0.95);
    border-radius: 20px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
    padding: 2rem;
    position: relative;
    z-index: 2;
    transform: translateY(0);
    transition: all 0.3s ease;

    &:hover {
        transform: translateY(-5px);
        box-shadow: 0 12px 40px rgba(0, 0, 0, 0.15);
    }
}

.login-header {
    text-align: center;
    margin-bottom: 2rem;

    .logo-icon {
        width: 60px;
        height: 60px;
        background: var(--gradient);
        border-radius: 12px;
        display: flex;
        align-items: center;
        justify-content: center;
        margin: 0 auto;
        color: white;
        font-size: 24px;
        font-weight: bold;
        box-shadow: 0 4px 15px rgba(77, 124, 254, 0.3);
    }
}

.form-group {
    margin-bottom: 1.5rem;
}

.login-input {
    border-radius: 10px;
    border: 1px solid #e0e0e0;
    transition: all 0.3s ease;

    &:focus {
        border-color: var(--primary);
        box-shadow: 0 0 0 2px rgba(77, 124, 254, 0.2);
    }
}

.login-button {
    background: var(--gradient);
    border: none;
    border-radius: 10px;
    transition: all 0.3s ease;

    &:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 15px rgba(77, 124, 254, 0.3);
    }
}

@keyframes float {
    0%, 100% {
        transform: translateY(0) rotate(0deg);
    }
    50% {
        transform: translateY(-20px) rotate(5deg);
    }
}

@media screen and (max-width: 768px) {
    .login-card {
        margin: 1rem;
        padding: 1.5rem;
    }

    .shape {
        opacity: 0.05;
    }
}
</style> 